﻿using System;
using Quartz;
using Quartz.Impl;

namespace NetFrame.JobService
{
    /// <summary>
    /// 任务管理器
    /// </summary>
    public class SchedulerManager
    {

        private static SchedulerManager scheduler;
        private static readonly object ObjLock = new object();
        private  readonly IScheduler _scheduler;

        private static string _defaultJobGroup = "DefaultJobGroup";
        private static string _defaultTrigerGroup = "DefaultJobGroup";

        public static string DefaultJobGroup
        {
            get
            {
                return _defaultJobGroup;
            }
        }

        public IScheduler Scheduler
        {
            get
            {
                return _scheduler;
            }
        }

        /// <summary>
        /// 构建单例
        /// </summary>
        public static SchedulerManager Instance
        {
            get
            {
                if (scheduler == null)
                {
                    lock (ObjLock)
                    {
                        if (scheduler == null)
                        {
                            return scheduler = new SchedulerManager();
                        }
                    }
                }
                return scheduler;
            }
        }

        private SchedulerManager()
        {
            Common.Logging.LogManager.Adapter =new  Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter(new System.Collections.Specialized.NameValueCollection());
            ISchedulerFactory sf = new StdSchedulerFactory();
            _scheduler = sf.GetScheduler();
               
           
        }
        /// <summary>
        /// Add Job
        /// </summary>
        /// <param name="JobDetail">Conveys the detail properties of a given IJob instance</param>
        private void AddJob(JobDetail jobDetail)
        {
            _scheduler.AddJob(jobDetail, true);
        }
        /// <summary>
        /// 增加job	
        /// </summary>
        /// <remarks>
        /// 刘新高于2010-6-22创建
        /// </remarks>
        /// <param name="ed">job委托</param>
        /// <param name="jobname">job名字.</param>
        public void AddJob(ExecteDelegate ed, string jobname)
        {
            JobAgent ja = new JobAgent(ed, jobname, _defaultJobGroup);
            AddJob(typeof(JobAgent), jobname);
        }
        /// <summary>
        /// Create a JobDetail  with the given name, and group, and the default settings of all the other properties. 
        /// </summary>
        /// <param name="jobtype">a given IJob instance's type</param>
        /// <param name="jobname">the given name</param>
        /// <param name="groupname">the group name,if is null it will use Scheduler.DefaultGroup</param>
        private void  AddJob(Type jobtype,string jobname)
        {
            JobDetail jobdetail = new JobDetail(jobname, _defaultJobGroup, jobtype);
            AddJob(jobdetail);
        }
        /// <summary>
        /// 增加	CronTrigger
        /// </summary>
        /// <remarks>
        /// 刘新高于2010-6-22创建
        /// </remarks>
        /// <param name="trigerName">triger名字</param>
        /// <param name="groupName">triger组名字.</param>
        /// <param name="jobName">job名字</param>
        /// <param name="jobGroup">job组.</param>
        /// <param name="regCron">triger cron如：0/50 * * * * ?</param>
        public void AddCronTrigger(string trigerName,string jobName,string regCron)
        {

            AddTrigger(new CronTrigger(trigerName, _defaultTrigerGroup, jobName, _defaultJobGroup, regCron));

        }
        /// <summary>
        /// 应用于转换trigger	
        /// </summary>
        /// <remarks>
        /// 刘新高于2010-6-23创建
        /// </remarks>
        /// <param name="trigerName">Name of the triger.</param>
        /// <param name="groupName">Name of the group.</param>
        /// <param name="jobName">Name of the job.</param>
        /// <param name="jobGroup">The job group.</param>
        /// <param name="regCron">The reg cron.</param>
        /// <param name="ed">The ed.</param>
        public void AddCronTriggerJob(string trigerName,  string jobName,  string regCron,ExecteDelegate ed)
        {
            //切换trigger
            _scheduler.UnscheduleJob(trigerName, _defaultTrigerGroup);
            AddJob(ed, jobName);
            AddTrigger(new CronTrigger(trigerName, _defaultTrigerGroup, jobName, _defaultJobGroup, regCron));

        }
        /// <summary>
        /// 停止一个任务.	
        /// </summary>
        /// <remarks>
        /// 刘新高于2010-6-23创建
        /// </remarks>
        /// <param name="trigerName">Name of the triger.</param>
        /// <param name="groupName">Name of the group.</param>
        public void UnscheduleJob(string trigerName)
        {
            _scheduler.UnscheduleJob(trigerName, _defaultTrigerGroup);
        }
        private void AddTrigger(Trigger trigger)
        {
            
            _scheduler.ScheduleJob(trigger);
        }
        
        private void AddScheduleJob(JobDetail jobDetail, Trigger trigger)
        {
            _scheduler.ScheduleJob(jobDetail, trigger);
        }
        /// <summary>
        /// 开始计划任务
        /// </summary>
        public void Start()
        {
            if (!_scheduler.IsStarted)
            {
                _scheduler.Start();
            }
        }
        /// <summary>
        /// 关闭计划任务
        /// </summary>
        public void ShutDown()
        {
            if (!_scheduler.IsShutdown)
            {
                _scheduler.Shutdown(true);
            }
        }

        /// <summary>
        /// 暂停计划任务
        /// </summary>
        public void PauseAll()
        {
            _scheduler.PauseAll();
        }

        /// <summary>
        /// 重启计划任务
        /// </summary>
        public void ResumeAll()
        {
            _scheduler.ResumeAll();
        }

        /// <summary>
        /// 暂停Job
        /// </summary>
        /// <param name="jobname"></param>
        public void PauseJob(string jobname)
        {
            _scheduler.PauseJob(jobname, _defaultJobGroup);
        }

        /// <summary>
        /// 重启Job
        /// </summary>
        /// <param name="jobname"></param>
        public void ResumeJob(string jobname)
        {
            _scheduler.ResumeJob(jobname, _defaultJobGroup);
        }

    }
}
